<?php
class Framework_DB_Message extends Framework_DB_Common
{
	public $tables = array('SiteUser',
	                       'MsgCategory',
	                       'PrivateMsg');
	                          
	public $refs = array(
		array(
			'ftable' => 'PrivateMsg',
			'fkey' => 'idSiteUserRec',
			'rtable' => 'SiteUser',
			'rkey' => 'idSiteUser',
			'on_update' => 'cascade',
			'on_delete' => 'cascade'
		),
		
		array(
			'ftable' => 'PrivateMsg',
			'fkey' => 'idSiteUserSen',
			'rtable' => 'SiteUser',
			'rkey' => 'idSiteUser',
			'on_update' => 'cascade',
			'on_delete' => 'cascade'
		),
			
		array(
			'ftable' => 'PrivateMsg',
			'fkey' => 'idMsgCategory',
			'rtable' => 'MsgCategory',
			'rkey' => 'idMsgCategory',
		)
	);
	
	public $sql = array(
		'getMessages' => array(
			'select'  => 'PrivateMsg.idPrivateMsg AS idPrivateMsg,
		                  PrivateMsg.messageTitle AS messageTitle, 
		                  PrivateMsg.timeSent AS timeSent, 
		                  PrivateMsg.readStatus AS readStatus, 
 		                  SiteUser.userName AS userName, 
    	                  SiteUser.siteDomain AS domain',
			'from'    => 'PrivateMsg, MsgCategory, SiteUser',
			'where'   => 'PrivateMsg.idMsgCategory = MsgCategory.idMsgCategory',
			'get'     => 'all'
		),
			
		'getUnreadMessages' => array(
			'select' => '*',
			'from'   => 'PrivateMsg',
			'where'  => 'readStatus = 0 
                         AND recieverStatus = 1 
                         AND idSiteUserRec = ?'
		),
			
		'getSentMessage' => array(
			'select' => '*',
			'from'   => 'PrivateMsg, SiteUser',
			'where'  => 'PrivateMsg.idSiteUserRec = SiteUser.idSiteUser
						AND idPrivateMsg = ? 
						AND idSiteUserSen = ?',
			'get'	 => 'row'
		),
			
		'getMessage' => array(
			'select' => '*',
			'from'   => 'PrivateMsg, SiteUser',
			'where'  => 'PrivateMsg.idSiteUserSen = SiteUser.idSiteUser
						AND idPrivateMsg = ? 
						AND idSiteUserRec = ?',
			'get'	 => 'row'
		),
			
		'getMessageCategory' => array(
			'select' => 'idMsgCategory',
			'from'   => 'MsgCategory',
			'where'  => 'val = ?',
			'get'    => 'one'
		),
	    	
	);
	
	public function __construct($name)
	{
		parent::__construct($name);
	}
	
	
}